package boundary;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JComboBox;
import javax.swing.JButton;

import client.Client;

import entity.Customer;
import entity.Order;
import entity.ParkingLot;
import entity.Prices;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class OneTimeOrderInAdvanceWin extends JPanel {

	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	JFrame frame;
	JPanel lastPanel;
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}
	
	
	

	/**
	 * Create the frame.
	 */
	public OneTimeOrderInAdvanceWin() {
		setBounds(100, 100, 550, 373);
		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(null);
		
		JLabel lblNewOrder = new JLabel("New Order");
		lblNewOrder.setBounds(196, 11, 136, 14);
		add(lblNewOrder);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 63, 46, 14);
		add(lblId);
		
		textField = new JTextField();
		textField.setBounds(137, 60, 166, 20);
		add(textField);
		textField.setColumns(10);
		
		JLabel lblCarNumber = new JLabel("Car Number:");
		lblCarNumber.setBounds(10, 100, 95, 14);
		add(lblCarNumber);
		
		textField_1 = new JTextField();
		textField_1.setBounds(137, 95, 166, 20);
		add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblLeavingDate = new JLabel("Leaving Date:");
		lblLeavingDate.setBounds(10, 235, 95, 14);
		add(lblLeavingDate);
		
		JLabel lblArrivalDate = new JLabel("Arrival Date:");
		lblArrivalDate.setBounds(10, 207, 95, 14);
		add(lblArrivalDate);
		
		JLabel lblParkingLotNumber = new JLabel("Parking Lot Number:");
		lblParkingLotNumber.setBounds(10, 277, 122, 14);
		add(lblParkingLotNumber);
		
		final  JComboBox<String> comboBox = new JComboBox<String>();
		comboBox.setBounds(158, 210, 46, 20);
		add(comboBox);
		
		final JComboBox<String> comboBox_1 = new JComboBox<String>();
		comboBox_1.setBounds(214, 210, 52, 20);
		add(comboBox_1);
		
		final JComboBox<String> comboBox_2 = new JComboBox<String>();
		comboBox_2.setBounds(276, 213, 46, 20);
		add(comboBox_2);
		
		final JComboBox<String> comboBox_3 = new JComboBox<String>();
		comboBox_3.setBounds(158, 238, 46, 20);
		add(comboBox_3);
		
		final JComboBox<String> comboBox_4 = new JComboBox<String>();
		comboBox_4.setBounds(214, 238, 52, 20);
		add(comboBox_4);
		
		final JComboBox<String> comboBox_5 = new JComboBox<String>();
		comboBox_5.setBounds(276, 241, 46, 20);
		add(comboBox_5);
		
		final  JComboBox<String> comboBox_6 = new  JComboBox<String>();
		comboBox_6.setBounds(158, 274, 76, 20);
		add(comboBox_6);
		
		final JComboBox<String> comboBox_8 = new JComboBox<String>();
		comboBox_8.setBounds(434, 210, 46, 20);
		add(comboBox_8);
		
		final JComboBox<String> comboBox_9 = new JComboBox<String>();
		comboBox_9.setBounds(378, 210, 46, 20);
		add(comboBox_9);
		
		final JComboBox<String> comboBox_10 = new JComboBox<String>();
		comboBox_10.setBounds(378, 238, 46, 20);
		add(comboBox_10);
		
		final JComboBox<String> comboBox_11 = new JComboBox<String>();
		comboBox_11.setBounds(434, 238, 46, 20);
		add(comboBox_11);
		
		JButton btnProceed = new JButton("Proceed");
		btnProceed.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(isValidDate(comboBox_1.getSelectedItem() + "/" + comboBox_2.getSelectedItem() + "/" + comboBox.getSelectedItem()) &&
					isValidDate(comboBox_4.getSelectedItem() + "/" + comboBox_5.getSelectedItem() + "/" + comboBox_3.getSelectedItem()))
				{
				Date arrival=new Date(Integer.valueOf((String)comboBox.getSelectedItem()), Integer.valueOf((String)comboBox_1.getSelectedItem()), Integer.valueOf((String)comboBox_2.getSelectedItem()), Integer.valueOf((String)comboBox_9.getSelectedItem()), Integer.valueOf((String)comboBox_8.getSelectedItem()),0);
				Date leaving=new Date(Integer.valueOf((String)comboBox_3.getSelectedItem()), Integer.valueOf((String)comboBox_4.getSelectedItem()), Integer.valueOf((String)comboBox_5.getSelectedItem()), Integer.valueOf((String)comboBox_10.getSelectedItem()), Integer.valueOf((String)comboBox_11.getSelectedItem()),0);
				Order order=new Order(textField.getText(), textField_1.getText(), Integer.valueOf((String)comboBox_6.getSelectedItem()), arrival, leaving, textField_2.getText());
				Customer [] customers=Client.getCustomers();
				Customer customer = null;
				for (int i=0; i<customers.length; i++)
					if (customers[i].getCostumer_id().equals(textField.getText()))
						customer=customers[i];
				int plNum=Integer.valueOf((String)comboBox_6.getSelectedItem());
				ParkingLot pl=Client.getParkingLots_sorted()[plNum-1];
				if (order.Validate(textField.getText(), textField_1.getText(), plNum, arrival, leaving, textField_2.getText()))
				{
					if(pl.addOrder(arrival, leaving))
					{
						Client.saveOrderToDB(order);
						Prices p=Client.getCurrentPrices(plNum);
						double payment=p.getOrderedParkingPlace()*((leaving.getTime()-arrival.getTime())/1000*60*60);
						customer.AddBalance(-payment);
						JOptionPane.showMessageDialog(null,"your order is registered in the system, your account is billed for " + payment +"$","order",JOptionPane.INFORMATION_MESSAGE);
					}
					else
						JOptionPane.showMessageDialog(null,"no available free parking spots during the requested time","order",JOptionPane.ERROR_MESSAGE);
				}
				else
					JOptionPane.showMessageDialog(null,"wronge input","order",JOptionPane.ERROR_MESSAGE);

			}
				else
					JOptionPane.showMessageDialog(null,"illegal date","one time parking",JOptionPane.ERROR_MESSAGE);
			}
		});
		btnProceed.setBounds(93, 322, 89, 23);
		add(btnProceed);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setContentPane(lastPanel);
			}
		});
		btnCancel.setBounds(313, 322, 89, 23);
		add(btnCancel);
		
		JLabel lblEmailAddress = new JLabel("Email Address");
		lblEmailAddress.setBounds(10, 134, 95, 14);
		add(lblEmailAddress);
		
		textField_2 = new JTextField();
		textField_2.setBounds(137, 130, 166, 20);
		add(textField_2);
		textField_2.setColumns(10);
		
		JLabel lblyyMm = new JLabel("(YY   .   MM   .   DD)");
		lblyyMm.setBounds(192, 185, 112, 14);
		add(lblyyMm);
		
		JLabel lblyyMm_1 = new JLabel("(HH   .   MM)");
		lblyyMm_1.setBounds(402, 185, 109, 14);
		add(lblyyMm_1);
		
		Date now=new Date();
		for (int i=0; i<10;i++)
		{
			comboBox.addItem(String.valueOf(1900+now.getYear()+i));
			comboBox_3.addItem(String.valueOf(1900+now.getYear()+i));
		}
		for (int i=1; i<=12;i++)
		{
			comboBox_1.addItem(String.valueOf(i));
			comboBox_4.addItem(String.valueOf(i));
		}
		for (int i=1; i<=31;i++)
		{
			comboBox_2.addItem(String.valueOf(i));
			comboBox_5.addItem(String.valueOf(i));
		}
		for (int i=0; i<=23;i++)
		{
			comboBox_9.addItem(String.valueOf(i));
			comboBox_10.addItem(String.valueOf(i));
		}
		for (int i=0; i<=3;i++)
		{
			comboBox_8.addItem(String.valueOf(i*15));
			comboBox_11.addItem(String.valueOf(i*15));
		}
		for (int i=1; i<=6;i++)
		{
			comboBox_6.addItem(String.valueOf(i));
		}
		
	}
	private static boolean isValidDate(String input) {
        String formatString = "MM/dd/yyyy";

        try {
            SimpleDateFormat format = new SimpleDateFormat(formatString);
            format.setLenient(false);
            format.parse(input);
        } catch (ParseException e) {
            return false;
        } catch (IllegalArgumentException e) {
            return false;
        }

        return true;
    }
}
